import { _decorator, Animation, Component, Node, v2, Vec2 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Knight')
export class Knight extends Component {
    /**起始点 */
    startPos:Vec2 = v2(0, 0)
    /**终点 */
    endPos:Vec2 = v2(0, 0)
    /**方向 */
    dir:Vec2 = v2(0, 0)

    animation:Animation

    protected onLoad(): void {
        this.animation = this.node.getComponent(Animation)
    }

    start() {

    }

    update(deltaTime: number) {
        
    }

    setData(sPos:Vec2, ePos:Vec2){
        this.startPos = sPos
        this.endPos = ePos
        this.dir = ePos.clone().subtract(sPos).normalize()

        let hu = Math.atan2(this.dir.y, this.dir.x)
        // 角度= 弧度 * 180 / pi
        // 弧度= 角度 / 180 * pi
        let angle = hu * 180 / Math.PI

        if(-22.5 <= angle && angle <= 22.5 ){
            // 0 右
            this.animation.play('right')
        }
        if(22.5 <= angle && angle <= 67.5 ){
            // 45 右上
            this.animation.play('rightUp')
        }
        if(67.5 <= angle && angle <= 112.5 ){
            // 90 上
            this.animation.play('up')
        }
        if(112.5 <= angle && angle <= 157.5 ){
            // 135 左上
            this.animation.play('leftUp')
        }
        if(157.5 <= angle || angle <= -157.5){
            // 180 左
            this.animation.play('left')
        }
        if(-157.5 <= angle && angle <= -112.5 ){
            // -135 左下
            this.animation.play('leftDown')
        }
        if(-112.5 <= angle && angle <= -67.5 ){
            // -90 下
            this.animation.play('down')
        }
        if(-67.5 <= angle && angle <= -22.5 ){
            // -45 右下
            this.animation.play('rightDown')
        }
    }
}


